package com.kakavr.page;

import java.io.Serializable;
import java.util.List;

import com.github.pagehelper.Page;

/**
 * 分页
 * 
 * @author gang.yang
 */
public class Pagination<T> implements Serializable{
	private static final long serialVersionUID = 1L;
	/**
	 * 一页数据默认10条
	 */
	private int pageSize = 10;
	/**
	 * 当前页码
	 */
	private int page = 1;

	/**
	 * 上一页
	 */
	private int upPage;

	/**
	 * 下一页
	 */
	private int nextPage;
	/**
	 * 一共有多少条数据
	 */
	private long total = 0;

	/**
	 * 一共有多少页
	 */
	private int totalPage = 1;
	/**
	 * 数据集合
	 */
	private List<T> Listdatas;

	
	/**
	 * 当前页的数量 <= pageSize，该属性来自ArrayList的size属性
	 */
	private int size;

	/**
	 * 获取第一条记录位置
	 * 
	 * @return
	 */
	public int getFirstResult() {
		return (this.getPage() - 1) * this.getPageSize();
	}

	/**
	 * 获取最后记录位置
	 * 
	 * @return
	 */
	public int getLastResult() {
		return this.getPage() * this.getPageSize();
	}

	/**
	 * 计算一共多少页
	 */
	public void setTotalPage() {
		this.totalPage = (int) ((this.total % this.pageSize > 0) ? (this.total / this.pageSize + 1)
				: this.total / this.pageSize);
	}

	/**
	 * 设置 上一页
	 */
	public void setUpPage() {
		this.upPage = (this.page > 1) ? this.page - 1 : this.page;
	}

	/**
	 * 设置下一页
	 */
	public void setNextPage() {
		this.nextPage = (this.page == this.totalPage) ? this.page : this.page + 1;
	}

	public int getNextPage() {
		return nextPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getUpPage() {
		return upPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int pageNo) {
		this.page = pageNo;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long totalCount) {
		this.total = totalCount;
	}

	public List<T> ListgetDatas() {
		return Listdatas;
	}

	public void setDatas(List<T> Listdatas) {
		this.Listdatas = Listdatas;
		setSize(Listdatas.size());
	}

	public List<T> getDatas() {
		return Listdatas;
	}

	public Pagination(int pageNo, int pageSize, long totalCount) {
		this.setPage(pageNo);
		this.setPageSize(pageSize);
		this.setTotal(totalCount);
		this.init();
	}

	public Pagination(List<T> list) {
		if (list instanceof Page) {
			Page<T> page = (Page<T>) list;
			this.page = page.getPageNum();
			this.pageSize = page.getPageSize();
			this.total = page.getTotal();
			this.totalPage = page.getPages();
			this.Listdatas = page;
			this.size = page.size();
		}

	}
	
	public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

	/**
	 * 初始化计算分页
	 */
	private void init() {
		this.setTotalPage();// 设置一共页数
		this.setUpPage();// 设置上一页
		this.setNextPage();// 设置下一页
	}
}
